home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-05  |  2.3 KB  |  77 lines

  1. /*
  2.  *   The search routine takes a directory list, separated by PATHSEP, and
  3.  *   tries to open a file.  Null directory components indicate current
  4.  *   directory. if the file SUBDIR exists and the file is a font file,
  5.  *   it checks for the file in a subdirectory named the same as the font name.
  6.  *   Returns the open file descriptor if ok, else NULL.
  7.  */
  8. #include "structures.h" /* The copyright notice in that file is included too! */
  9. #include <stdio.h>              /* for FILE and fopen */
  10. #ifdef SYSV
  11. #include <string.h>
  12. #define MAXPATHLEN (128)
  13. #else   /* ~SYSV */
  14. #include <sys/param.h>          /* for MAXPATHLEN */
  15. #include <strings.h>            /* for strlen */
  16. #endif  /* ~SYSV */
  17. /*
  18.  *
  19.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  20.  */
  21.  
  22. #ifdef DEBUG
  23. extern integer debug_flag;
  24. #endif  /* DEBUG */
  25.  
  26. /* argument to fopen */
  27. #define READ            "r"
  28. /* directories are separated in the path by PATHSEP */
  29. #define PATHSEP         ':'
  30. /* DIRSEP is the char that separates directories from files */
  31. #define DIRSEP          '/'
  32. extern void error() ;
  33.  
  34. FILE *
  35. search(path, file)
  36.         char *path, *file ;
  37. {
  38.    register char *nam ;                 /* index into fname */
  39.    register FILE *fd ;                  /* file desc of file */
  40.    char fname[MAXPATHLEN] ;             /* to store file name */
  41.  
  42.    if (*file == DIRSEP) {               /* if full path name */
  43.       if ((fd=fopen(file,READ)) != NULL)
  44.          return(fd) ;
  45.       else
  46.          return(NULL) ;
  47.    }
  48.  
  49.    do {
  50.       /* copy the current directory into fname */
  51.       nam = fname;
  52.       /* copy till PATHSEP */
  53.       while (*path != PATHSEP && *path) *nam++ = *path++;
  54.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  55.       *nam++ = DIRSEP;                  /* add separator */
  56.  
  57.       (void)strcpy(nam,file);                   /* tack the file on */
  58.  
  59.       /* belated check -- bah! */
  60.       if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
  61.          error("! overran allocated storage in search()");
  62.  
  63. #ifdef DEBUG
  64.       if (dd(D_PATHS))
  65.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  66. #endif
  67.       if ((fd=fopen(fname,READ)) != NULL)
  68.          return(fd);
  69.  
  70.    /* skip over PATHSEP and try again */
  71.    } while (*(path++));
  72.  
  73.    return(NULL);
  74.  
  75. }               /* end search */
  76.  
  77.